<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<head>
<link rel="stylesheet" type="text/css" href="<%=basePath%>images/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="<%=basePath%>images/themes/icon.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="<%=basePath%>js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery.easyui.min.js"></script>
<script type="text/javascript">
	$(function(){
		initTable();
	});
	//初始化list表
	function initTable(){
		$('#listGrid').treegrid({
			title:'模块',
			fit:true,
			//singleSelect:true,
			url:'<%=basePath%>model/list',
			idField:'id',
			treeField:'name',
			columns:[[
				{checkbox:true},
				{field:'id',title:'ID',width:100,align:'center',hidden:true},
				{field:'name',title:'模块名',width:200,align:'left'},
				{field:'remark',title:'备注',width:300,align:'center'}
			]],
			//rownumbers:true,
			toolbar:'#tb'
		});
	}
	
	//添加
	function add(){
		$('#editDialog').dialog({
			width:300,
			height: 200,
			resizable:true,
			title:'添加',
			modal:true
		});
		$('#editDialog').dialog("open");
		$('#editDialog form').form('clear');
		getModelSelect();
	}
	//关闭编辑框
	function closeEdit(){
		$('#editDialog').dialog("close");
	}
	//删除
	function del(){
		var selections = $('#listGrid').datagrid('getSelections');
		if(selections.length>0){
			var ids=[];
			var names=[];
			for(var i=0;i<selections.length;i++){
				ids.push(selections[i].id);
				names.push(selections[i].name)
			}
			$.messager.confirm('删除', '确定删除 '+names.join(',')+' 吗?', function(r){
				if (r){
					$.ajax({
						"url":"<%=basePath%>model/delete",
						"dataType":"json",
						"type":"post",
						"data":{
							"id":ids.join(",")
						},
						"success":function(data){
							if(!data.success){
								$.messager.alert("错误",data.code,"error");
							}else{
								$('#listGrid').datagrid('reload');
								$.messager.alert("成功","保存成功","info");
								closeEdit();
							}
						},
						"error":function(xhr,err,exp){
							$.messager.alert("错误","系统错误，或无权限访问","error");
						}
					});
				}
			});
			
		}else{
			$.messager.alert("警告","请选择要删除的记录","warning");
		}
	}
	//更新
	function update(){
		var selections = $('#listGrid').datagrid('getSelections');
		if(selections.length==1){
			$('#editDialog').dialog({
				width:300,
				height: 200,
				resizable:true,
				title:'修改',
				modal:true
			});
			$('#editDialog').dialog("open");
			$('#editDialog form').form('clear');
			getModelSelect(selections[0]._parentId);
			$('#parentId').combobox("disable");
			$('#editDialog form').form('load',selections[0]);
		}else{
			$.messager.alert("警告","请选择一条记录","warning");
		}
	}
	function getModelSelect(_parentId){
		$('#parentId').combobox({  
		    url:'<%=basePath%>model/select',  
		    valueField:'id',  
		    textField:'name',
		    onLoadSuccess:function(){
		    	$('#parentId').combobox('setValue',_parentId);
		    }
		});
	}
	
	//搜索
	function query(){
		$('#listGrid').datagrid('load',{
			"name": $("#nameq").val(),
			"remark": $("#remarkq").val()
		});
	}
	//保存
	function save(){
		if(!$('#form').form('validate')){
			return;
		}
		$.messager.confirm('添加', '确定提交吗?', function(r){
			if (r){
				$.ajax({
					"url":"<%=basePath%>model/save",
					"dataType":"json",
					"type":"post",
					"data":{
						"id":$("#id").val(),
						"name":$("#name").val(),
						"parentId":($("#parentId").combobox('getValue')=="")?undefined:$("#parentId").combobox('getValue'),
						"remark":$("#remark").val()
					},
					"success":function(data){
						if(!data.success){
							$.messager.alert("错误",data.code,"error");
						}else{
							$('#listGrid').treegrid('reload');
							closeEdit();
						}
					},
					"error":function(xhr,err,exp){
						$.messager.alert("错误","系统错误，或无权限访问","error");
					}
				});
			}
		});
	}
	//功能管理
	function functionManager(){
		var selections = $('#listGrid').datagrid('getSelections');
		if(selections.length==1){
			$("#fun").attr("src",'<%=basePath%>function/'+selections[0].id);
			$('#function').dialog({
				width:800,
				height: 400,
				resizable:true,
				title:'功能管理',
				modal:true
			});
			//$('#function').dialog('refresh', 'new_content.php');
		}else{
			$.messager.alert("警告","请选择一条记录","warning");
		}
	}
</script>
</head>
<body>
	<shiro:hasPermission name="/model">
	<table id="listGrid"></table>
	<div id="tb" style="padding:5px;height:auto">  
	    <div style="margin-bottom:5px">  
			<table cellpadding="0" cellspacing="0">
				<tr>
					<td>模块名:<input id="nameq" type="text"/></td>
					<td>备注:<input id="remarkq" type="text"/></td>
					<td><a id="search"  class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="query()">搜索</a></td>
				</tr>
			</table>
	    </div>  
		<div>    
			<table  cellpadding="0" cellspacing="0" >
				<tr>
					<td>
						<shiro:hasPermission name="/model/save">
						<a id="add" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="add()">添加</a>
						<a id="update" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="update()">更新</a>
						</shiro:hasPermission>
						<shiro:hasPermission name="/function">
						<a id="func" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="functionManager()">功能管理</a>
						</shiro:hasPermission>
						<shiro:hasPermission name="/model/delete">
						<a id="delet" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="del()">删除</a>	
						</shiro:hasPermission>
					</td>
				</tr>
			</table>
		</div>
	</div>
	<shiro:hasPermission name="/model/save">
	<!-- 编辑 DIALOG ###################################################################### -->
	<div id="editDialog" class="easyui-dialog" data-options="buttons:'#bb',closed:true" style="padding: 5px;">
		<form id="form" action="#">
			<input type="hidden" id="id" name="id"/>
			<table>
				<tr>
					<td>模块名:</td>
					<td>
					<input class="easyui-validatebox" id="name" name="name" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td>父模块:</td>
					<td>
					<input class="easyui-validatebox" id="parentId" name="parentId">
					</td>
				</tr>
				<tr>
					<td>备注:</td>
					<td>
					<input class="easyui-validatebox" id="remark" name="remark">
					</td>
				</tr>
			</table>
		</form>
	</div>
	<div id="bb">
		<a href="#" class="easyui-linkbutton" onclick="save()">保存</a>
		<a href="#" class="easyui-linkbutton" onclick="closeEdit()">关闭</a>
	</div>
	</shiro:hasPermission>
	<shiro:hasPermission name="/function">
	<div id="function" style="padding: 5px;">
	<iframe id="fun" scrolling="auto" frameborder="0"  src="" style="width:100%;height:100%;"></iframe>
	</div>
	</shiro:hasPermission>
	</shiro:hasPermission>
</body>
</html>